package com.fuchuang.seckillsystem.service.impl;

import com.fuchuang.seckillsystem.dto.RecordQuery;
import com.fuchuang.seckillsystem.entity.OverRecord;
import com.fuchuang.seckillsystem.mapper.OverRecordMapper;
import com.fuchuang.seckillsystem.service.IOverRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OverRecordService implements IOverRecordService {
    
    @Resource
    private OverRecordMapper overRecordMapper;
    
    
    @Override
    public Integer getOverCount(RecordQuery recordQuery) {
        //获取用户规定年限内全部逾期记录
        List<OverRecord> recordList = overRecordMapper.queryOverRecordByIdcard(recordQuery);
        //进行筛选
        for (OverRecord overRecord : recordList) {
            //如果金额小于规定逾期金额 并且在规定时间内还款
            if (overRecord.getRepayTime() != null && overRecord.getMoney().compareTo(recordQuery.getOverdueMoney()) < 0) {
                int days = (int) ((overRecord.getRepayTime().getTime() - overRecord.getStartTime().getTime()) / (1000 * 3600 * 24));
                if (days <= recordQuery.getOverdueDay()) {
                    recordList.remove(overRecord);
                }
            }
        }
        return recordList.size();
    }
}
